<%@page import="multiface.awwadco.jpa.PersistenceManager"%>
<%@page import="org.apache.commons.logging.LogFactory"%>
<%@page import="org.apache.commons.logging.Log"%>
<%@page import="multiface.web.util.ResizeBufferedImage"%>
<%@page import="multiface.web.util.CreateThumbnail"%>
<%@page import="javax.swing.ImageIcon"%>
<%@page import="java.awt.image.BufferedImage"%>
<%@page import="java.awt.Image"%>
<%@page import="java.util.Enumeration"%>
<%@page import="org.apache.catalina.util.Enumerator"%>
<%@page import="multiface.awwadco.jpa.Mimetype"%>
<%@page import="multiface.awwadco.jpa.Prodotto"%>
<%@page import="java.sql.Timestamp"%>
<%@page import="java.util.Date"%>
<%@page import="javax.persistence.Persistence"%>
<%@page import="multiface.awwadco.jpa.Utente"%>
<%@page import="javax.persistence.EntityManager"%>
<%@page import="javax.persistence.EntityManagerFactory"%>
<%@page import="multiface.awwadco.jpa.File"%>
<%@page import="multiface.web.util.ImageConverter"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.FileItemFactory"%>

<%
    EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
    EntityManager em = emf.createEntityManager();
    
    Log log = LogFactory.getLog("addfilesjsp");
    

    try {

        Prodotto p = em.find(Prodotto.class, Integer.parseInt(request.getParameter("pid")));
        Date d = new Date();

        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        List<FileItem> items = upload.parseRequest(request);
        //Sy_stem.out.println("Size List FileItem: " + items.size());
        for (FileItem item : items) {
            if (!item.isFormField()) {
                // Metodi per resize delle immagini
                
                
                byte[] img_big = ResizeBufferedImage.resizeImageProporzionalWith_BytesToBytes(item.get(), 640);
                byte[] img_th = ResizeBufferedImage.resizeImageProporzionalWith_BytesToBytes(item.get(), 128);
                
                File f = new File();
                f.setBytes(img_big);
                f.setBytesTh(img_th);
                f.setCopertina(false);
                f.setDataora(new Timestamp(d.getTime()));
                f.setDimensione((int) img_big.length);
                
                // occorre verificare che il nome del file sia di massimo 255 caratteri
                // e di modificarlo in modo opportuno nel caso sia piu' lungo
                f.setNome(item.getName());
                String[] arr = item.getName().split("\\.");
                
                // occorre verificare che il tipo del file sia di massimo 255 caratteri
                // e di modificarlo in modo opportuno nel caso sia piu' lungo
                f.setTipo(em.find(Mimetype.class, arr[arr.length - 1]).getTipo());
                f.setProdottoIdprodotto(p);

                em.getTransaction().begin();
                em.persist(f);
                em.getTransaction().commit();
                em.refresh(f);

                out.print(f.getIdfile());
            }
        }
    } catch (Exception e) {
        log.error(e);
    } finally {
        em.close();
        //emf.close();
    }
%>